using System;
using HostComputerPlatform.Models;

namespace HostComputerPlatform.Services
{
    public class GlobalStateService
    {
        private static GlobalStateService _instance;
        public static GlobalStateService Instance => _instance ??= new GlobalStateService();

        public DeviceState CurrentState { get; private set; } = DeviceState.Idle;
        public event Action StateChanged;
        public event Action<string> ControlSignal;

        public void SetStateFromExternal(DeviceState state)
        {
            if (CurrentState != state)
            {
                CurrentState = state;
                StateChanged?.Invoke();
                ControlSignal?.Invoke(state.ToString().ToLower());
            }
        }

        public void Start()
        {
            if (CurrentState == DeviceState.Idle || CurrentState == DeviceState.Paused)
            {
                SetStateFromExternal(DeviceState.Running);
            }
        }
        public void Stop()
        {
            if (CurrentState == DeviceState.Running)
            {
                SetStateFromExternal(DeviceState.Paused);
            }
        }
        public void EmergencyStop()
        {
            if (CurrentState != DeviceState.EmergencyStopped)
            {
                SetStateFromExternal(DeviceState.EmergencyStopped);
            }
        }
        public void Resume()
        {
            if (CurrentState == DeviceState.EmergencyStopped)
            {
                SetStateFromExternal(DeviceState.Idle);
            }
        }
    }
} 